package com.kcl.dfss;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class DownLoadActivity extends Activity {

	private LinearLayout download_back;
	private ProgressBar download_bar;
	private TextView tv_progress,tv_upload;
	
	private HttpHandler handler;
//	private String url = "http://gdown.baidu.com/data/wisegame/3a248ac42c28d72c/baidushoujizhushou_16785204.apk";
//	private String path = Environment.getExternalStorageDirectory().toString()+"/bdsjzs.apk";
	
	private String url,path,server_romversion;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_down_load);
		
		download_back = (LinearLayout) findViewById(R.id.download_back);
		download_bar = (ProgressBar) findViewById(R.id.download_bar);
		tv_progress = (TextView) findViewById(R.id.tv_progress);
		tv_upload = (TextView) findViewById(R.id.tv_upload);
		
		download_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		
		Intent intent = getIntent();
		url = intent.getStringExtra("url");
		server_romversion = intent.getStringExtra("server_romversion");
		path = Environment.getExternalStorageDirectory().toString()+"/"+server_romversion;
		
//		File file = new File(path);
//		if(file.exists()){
//			Toast.makeText(DownLoadActivity.this, "文件已下载", Toast.LENGTH_SHORT).show();
//		}else{
			downLoadRom(url, path);			
//		}
		
	}
	
	/**
	 * rom下载
	 * @param url  test:http://gdown.baidu.com/data/wisegame/35b8a6e5b883b0c3/wannianli_26.apk
	 * @param sdPath
	 */
	private void downLoadRom(String url,String sdPath){
		HttpUtils http = new HttpUtils();
		handler = http.download(url, sdPath, true, false, new RequestCallBack<File>() {
			
			@Override
			public void onStart() {
				super.onStart();
				tv_upload.setText("正在连接...");
			}
			
			@Override
			public void onLoading(long total, long current,boolean isUploading) {
				super.onLoading(total, current, isUploading);
				download_bar.setProgress((int)((double)current/(double)total*100));
				tv_progress.setText((int)(current*100/total)+"%");
				tv_upload.setText("正在下载ROM，请勿退出软件...");
			}

			@Override
			public void onSuccess(ResponseInfo<File> arg0) {
				tv_upload.setText("下载完成");
				SharedPreferences preferences = getSharedPreferences("shared_login", 0);
				preferences.edit().putString("app_romversion", server_romversion).commit();
				startActivity(new Intent(DownLoadActivity.this, UpdateRomActivity.class));
				finish();
			}
			
			@Override
			public void onFailure(HttpException arg0, String arg1) {
//				tv_upload.setText(arg1);
				tv_upload.setText("下载失败，请重试");
			}
		});
	}

}
